package org.jugile.demo.domain;

import org.apache.log4j.Logger;
import org.jugile.daims.Bo;
import org.jugile.daims.DomainCore;
import org.jugile.demo.common.*;

public class DomainBase extends DomainCore {
	static Logger log = Logger.getLogger(DomainBase.class);
	
	public synchronized static Domain getDomain() { return getDomain(""); }
	public synchronized static Domain getDomain(String tag) {
		if (domain != null) return (Domain)domain;
		domain = new Domain();
		if (!"test".equals(tag)) {	
			if (hasdb()) {
				((Domain)domain).loadFromDB();
			}
		}
		World.init((Domain)domain, tag);
		return (Domain)domain;
	}

	
	// ====== objects =======
	public Book getBook(String id) { return getBook(parseLongSafe(id)); }	
	public Book getBook(long id) { return (Book)get(Book.class,id); }	
	public BookCollection getBooks() { return (BookCollection)getAll(Book.class); }
	protected Book createBook() { return (Book)createNew(Book.class); }
	
	public Visit getVisit(String id) { return getVisit(parseLongSafe(id)); }	
	public Visit getVisit(long id) { return (Visit)get(Visit.class,id); }	
	public VisitCollection getVisits() { return (VisitCollection)getAll(Visit.class); }
	protected Visit createVisit() { return (Visit)createNew(Visit.class); }
	
	public Author getAuthor(String id) { return getAuthor(parseLongSafe(id)); }	
	public Author getAuthor(long id) { return (Author)get(Author.class,id); }	
	public AuthorCollection getAuthors() { return (AuthorCollection)getAll(Author.class); }
	protected Author createAuthor() { return (Author)createNew(Author.class); }
	
	public Library getLibrary(String id) { return getLibrary(parseLongSafe(id)); }	
	public Library getLibrary(long id) { return (Library)get(Library.class,id); }	
	public LibraryCollection getLibraries() { return (LibraryCollection)getAll(Library.class); }
	protected Library createLibrary() { return (Library)createNew(Library.class); }
	

	// these are needed to know what to load from db
	protected Class<Bo>[] classes() { return classes; }
	private static Class[] classes = {
		Book.class,
		Visit.class,
		Author.class,
		Library.class,
	};

	
	// ====== old interface =======
	private Global global; // stored here for saving
	public Global global() { 
		if (global == null) global = Global.get(); 
		return global;
	}


}